package com.platform.manager.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.After;
import com.opensymphony.xwork2.interceptor.annotations.Before;
import com.platform.common.logger.annotation.EnableLogger;
import com.platform.common.mybatis.Page;
import com.platform.common.mybatis.context.ContextHolder;
import com.platform.domain.result.DataGirdData;
import com.platform.domain.result.ResultData;
import com.platform.domain.result.TreeData;
import com.platform.domain.result.TreeGridData;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.json.annotations.JSON;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 12-9-13
 * Time: 上午10:33
 * To change this template use File | Settings | File Templates.
 */
public class BaseAction extends ActionSupport {

    @EnableLogger
    protected Logger logger;
    //每页显示的记录数
    private String rows;
    //当前第几页
    private String page;

    protected ResultData result;

    protected List<TreeGridData> treeGridDatas;

    protected DataGirdData dataGirdData;

    protected List<TreeData> treeDatas;

    protected String jsonStr;

    @JSON
    public ResultData getResult() {
        return result;
    }

    public void setResult(ResultData result) {
        this.result = result;
    }

    @JSON
    public DataGirdData getDataGirdData() {
        return dataGirdData;
    }

    public void setDataGirdData(DataGirdData dataGirdData) {
        this.dataGirdData = dataGirdData;
    }

    @JSON
    public List<TreeGridData> getTreeGridDatas() {
        return treeGridDatas;
    }

    public void setTreeGridDatas(List<TreeGridData> treeGridDatas) {
        this.treeGridDatas = treeGridDatas;
    }

    @JSON
    public List<TreeData> getTreeDatas() {
        return treeDatas;
    }

    public void setTreeDatas(List<TreeData> treeDatas) {
        this.treeDatas = treeDatas;
    }

    public void setRows(String rows) {
        this.rows = rows;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public String getJsonStr() {
        return jsonStr;
    }

    public void setJsonStr(String jsonStr) {
        this.jsonStr = jsonStr;
    }

    /**
     * 获得翻页参数对象
     *
     * @return
     */
    protected Page getPage() {
        Page rollPage = new Page();
        //当前页
        int currentPage = Integer.parseInt((page == null || page == "0") ? "1" : page);
        rollPage.setCurrentPage(currentPage);
        logger.debug("当前页数：[" + currentPage + "]");
        //每页显示条数
        int pageSize = Integer.parseInt((rows == null || rows == "0") ? "1" : rows);
        rollPage.setPageSize(pageSize);
        logger.debug("当前页显示条数：[" + pageSize + "]");
        return rollPage;
    }

    /**
     * 打印调试信息
     *
     * @return
     */
    protected void print(Object message) {
        logger.debug(message);
    }

    @Before
    public void methodBefore(){
        //print("方法执行前");
        // 获取翻页参数
        Page rollPage = getPage();
        ContextHolder.setPage(rollPage);
    }

    @After
    public void methodAfter(){
        //print("方法执行后");
        // 清理过期的翻页参数
        ContextHolder.removePage();
    }

    //@BeforeResult
    public void methodBeforeResult(){
        print("返回result之前执行");
    }

}
